package com.idealighter.game.gamerecord.manage;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.assertions.HuohuaAssert;
import com.idealighter.game.gamerecord.convert.GameRecordDtoConvert;
import com.idealighter.game.gamerecord.dto.GamePlayerBillInfoDto;
import com.idealighter.game.gamerecord.dto.GameRoomInfoDto;
import com.idealighter.game.gamerecord.dto.GameRoundInfoDto;
import com.idealighter.game.gamerecord.dto.GameScoreInfoDto;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.player.manager.PlayerMgr;
import com.idealighter.game.player.service.bo.PlayerBo;
import com.idealighter.game.player.struct.Player;
import com.idealighter.game.server.db.datadb.dao.RoomCardNiuniuScoreDetailsDao;
import com.idealighter.game.server.db.datadb.dao.RoomCardRoomListDao;
import com.idealighter.game.server.db.datadb.dao.RoomCardScoreDao;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardNiuniuScoreDetailsDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardRoomListDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardScoreDomain;
import com.idealighter.utils.check.EmptyUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Singleton
public class GameRecordMgr {

  private RoomCardRoomListDao roomListDao;

  private RoomCardNiuniuScoreDetailsDao scoreDetailsDao;

  private RoomCardScoreDao scoreDao;

  private GameRecordMsgMgr msgMgr;

  @Inject
  private PlayerMgr playerMgr;

  /**
   * 构造函数.
   * 
   * @param roomListDao .
   * @param scoreDetailsDao .
   * @param scoreDao .
   * @param msgMgr .
   */
  @Inject
  public GameRecordMgr(RoomCardRoomListDao roomListDao,
      RoomCardNiuniuScoreDetailsDao scoreDetailsDao, RoomCardScoreDao scoreDao,
      GameRecordMsgMgr msgMgr) {
    this.roomListDao = roomListDao;
    this.scoreDetailsDao = scoreDetailsDao;
    this.scoreDao = scoreDao;
    this.msgMgr = msgMgr;
  }

  /**
   * 战绩列表.
   *
   * @author abin
   * @date 2018年5月25日 下午3:42:59
   * @param player 玩家.
   * @param type 战绩类型(1:我的战绩,2:我的房间战绩,3:牛友群战绩).
   * @param lastCreateTime 最后创建时间.
   * @param pageSize 页数.
   * @return 消息.
   */
  public ResMessage resGameRecordList(Player player, int type, long lastCreateTime, int pageSize) {
    HuohuaAssert.isTrue(type > 0 && type <= 3 && pageSize > 0);
    Date lastCreateTimeDate = null;
    if (lastCreateTime > 0) {
      lastCreateTimeDate = new Date(lastCreateTime);
    }
    long playerId = player.getId();
    List<Long> roomIds = null;
    if (type == 1) {
      roomIds = scoreDao.selectMyAllGameRecord(playerId, lastCreateTimeDate, pageSize);
    } else if (type == 2) {
      roomIds = scoreDao.selectMyRoomGameRecord(playerId, lastCreateTimeDate, pageSize);
    } else if (type == 3) {
      roomIds = scoreDao.selectMyGroupGameRecord(playerId, lastCreateTimeDate, pageSize);
    }

    List<GameRoomInfoDto> gameRoomInfoDtos = new ArrayList<>();
    if (EmptyUtil.listIsNotEmpty(roomIds)) {
      List<RoomCardRoomListDomain> roomListDomains = roomListDao.selectByIds(roomIds);
      if (EmptyUtil.listIsNotEmpty(roomListDomains)) {
        for (Long roomId : roomIds) {
          for (RoomCardRoomListDomain roomCardRoomListDomain : roomListDomains) {
            if (roomId.equals(roomCardRoomListDomain.getId())) {
              List<RoomCardScoreDomain> scoreDomains =
                  scoreDao.selectByRoomId(roomCardRoomListDomain.getId());
              List<GameScoreInfoDto> scoreInfoDtos =
                  GameRecordDtoConvert.INSTANCE.toGameScoreInfoDtos(scoreDomains);
              GameRoomInfoDto dto =
                  GameRecordDtoConvert.INSTANCE.toGameRoomInfoDto(roomCardRoomListDomain);
              dto.setScoreList(scoreInfoDtos);
              gameRoomInfoDtos.add(dto);
              break;
            }
          }
        }

      }
    }

    return msgMgr.resGameRecordList(gameRoomInfoDtos);
  }

  /**
   * 获取战绩详情.
   *
   * @author abin
   * @date 2018年5月25日 下午4:40:46
   * @param player 玩家.
   * @param roomId 房间id.
   * @return 消息.
   */
  public ResMessage resGameRecordDetail(Player player, long roomId) {
    List<RoomCardNiuniuScoreDetailsDomain> scoreDetailsDomains =
        scoreDetailsDao.selectByRoomId(roomId);

    List<GamePlayerBillInfoDto> dtos = new ArrayList<>();

    if (EmptyUtil.listIsNotEmpty(scoreDetailsDomains)) {
      long tempPlayerId = 0;
      GamePlayerBillInfoDto tempBillDto = null;
      List<GameRoundInfoDto> tempRoundDtos = null;
      // 先初始化玩家信息
      for (RoomCardNiuniuScoreDetailsDomain scoreDetailsDomain : scoreDetailsDomains) {
        if (scoreDetailsDomain.getPlayerId() != tempPlayerId) {
          tempPlayerId = scoreDetailsDomain.getPlayerId();

          PlayerBo nowPlayer = playerMgr.selectPlayer(tempPlayerId);

          if (nowPlayer == null) {
            continue;
          }

          tempBillDto = new GamePlayerBillInfoDto();
          tempBillDto.setPlayerAvatar(nowPlayer.getPlayerAvatar());
          tempBillDto.setPlayerId(nowPlayer.getId());
          tempBillDto.setPlayerName(nowPlayer.getPlayerName());
          tempBillDto.setPlayerSuperId(nowPlayer.getSuperId());
          tempBillDto.setTotalWin(0);

          tempRoundDtos = new ArrayList<>(20);
          tempBillDto.setRoundInfos(tempRoundDtos);
          dtos.add(tempBillDto);

        }
        if (tempBillDto != null && tempRoundDtos != null) {
          GameRoundInfoDto roundInfoDto =
              GameRecordDtoConvert.INSTANCE.toGameRoundInfoDto(scoreDetailsDomain);
          tempRoundDtos.add(roundInfoDto);
        }
      }
    }

    return msgMgr.resGameDetail(dtos);
  }

  /**
   * 房间结算 .
   *
   * @author abin
   * @date 2018年5月25日 下午3:57:52
   * @param player 玩家.
   * @param roomId 房间id.
   * @return 消息.
   */
  public ResMessage resGameBill(Player player, long roomId) {

    RoomCardRoomListDomain roomCardRoomListDomain = roomListDao.selectByPrimaryKey(roomId);

    List<RoomCardScoreDomain> scoreDomains =
        scoreDao.selectByRoomId(roomCardRoomListDomain.getId());
    List<GameScoreInfoDto> scoreInfoDtos =
        GameRecordDtoConvert.INSTANCE.toGameScoreInfoDtos(scoreDomains);
    GameRoomInfoDto dto = GameRecordDtoConvert.INSTANCE.toGameRoomInfoDto(roomCardRoomListDomain);

    dto.setScoreList(scoreInfoDtos);

    return msgMgr.resGameBill(dto);
  }



}
